const { series, parallel } = require('gulp')
const fs = require('fs')
    // series 并行（异步）  parallel 串行（同步）
    // exports.foo = (done) => {
    //     console.log('foo task working~')
    //     done() // 标识任务完成
    // }

// exports.default = (done) => {
//     console.log('default task working~')
//     done()
// }

// const gulp = require('gulp')

// gulp.task('bar', done => {
//     console.log('bar task working~')
//     done()
// })

// const task1 = done => {
//     setTimeout(() => {
//         console.log('task working~')
//         done()
//     }, 1000)
// }
// const task2 = done => {
//     setTimeout(() => {
//         console.log('task2 working~')
//         done()
//     }, 1000)
// }
// const task3 = done => {
//     setTimeout(() => {
//         console.log('task3 working~')
//         done()
//     }, 1000)
// }

// exports.foo = series(task1, task2, task3)
// exports.bar = parallel(task1, task2, task3)


exports.callback = done => {
    console.log('callback task')
    done()
}

exports.callback_error = done => {
    console.log('callback task')
    done(new Error('task failed'))
}

exports.promise = () => {
    console.log('promise task')
    return Promise.resolve()
}

exports.promise_error = () => {
    console.log('promise_error task')
    return Promise.reject(new Error('task error'))
}

const timeout = time => {
    return new Promise(resolve => {
        setTimeout(resolve, time)
    })
}

exports.async = async() => {
        await timeout(1000)
        console.log('async task')
    }
    // exports.stream = () => {
    //     const readStream = fs.createReadStream('package.json')
    //     const writeStream = fs.createWriteStream('temps.txt')
    //     readStream.pipe(writeStream)
    //     return readStream
    // }

exports.stream = done => {
    const readStream = fs.createReadStream('package.json')
    const writeStream = fs.createWriteStream('temps.txt')
    readStream.pipe(writeStream)
    readStream.on('end', () => {
        done()
    })
}